//
//  CoreCollectionViewController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/4.
//  Copyright © 2018年 longlong. All rights reserved.
//

#import "CoreCollectionViewController.h"
#import "CoreCollectionViewCell.h"
#import "CoreCollectionViewFlowLayout.h"

@interface CoreCollectionViewController ()
{
    ///开始滑动时的 Y 值
    CGFloat _startSlidingY;
    ///向上滑动
    BOOL _upSliding;
    ///第一次运行
    BOOL _firstRun;
}


@property (nonatomic ,strong) NSArray<NSString *> *titleArray;
@property (nonatomic ,strong) NSArray<NSString *> *controllerArray;
@end

@implementation CoreCollectionViewController

static NSString * const reuseIdentifier = @"CoreCollectionViewCell";

- (instancetype)init
{
    CoreCollectionViewFlowLayout *flatout = [[CoreCollectionViewFlowLayout alloc] init];
    flatout.itemSize = CGSizeMake(130, 130);
    return [self initWithCollectionViewLayout:flatout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[CoreCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.view.backgroundColor=[UIColor colorWithRed:38/255.0f green:44/255.0f blue:50/255.0f alpha:1];
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.backgroundColor=[UIColor colorWithRed:38/255.0f green:44/255.0f blue:50/255.0f alpha:1];
    _firstRun=YES;
    _upSliding=YES;

    [self.collectionView reloadData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_firstRun) {
        /*第一次运行显示界面时 所有显示的cell都放到最高层 后出现的就会压住前面的*/
        //上
        [collectionView bringSubviewToFront:cell];
    } else {
        
        if (_upSliding) {
            /* 向上滑时 后出现的就会压住前面的*/
            //上滑
            [collectionView bringSubviewToFront:cell];
        } else {
            /* 向下滑时 把每个将要出现的放到最底部 后出现的就会在前一个cell的下面*/
            //下滑
            [collectionView sendSubviewToBack:cell];
        }
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.textLable.text = self.titleArray[indexPath.row];
    return cell;
}

#pragma mark UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _startSlidingY = scrollView.contentOffset.y;
    _firstRun=NO;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y<_startSlidingY) {
        //下
        _upSliding=NO;
    } else  {
        //上
        _upSliding=YES;
    }
    _startSlidingY = scrollView.contentOffset.y;
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/


#pragma mark - Getters And Setters

//注解：OC 中我们很习惯的会用懒加载，当用到的时候才去加载它，但是实际上只要一个类实现了某个方法，就会被加载进内存。
- (NSArray<NSString *> *)titleArray
{
    if (_titleArray == nil)
    {
        _titleArray = @[@"关键字",@"Runtime",@"Runloop",@"消息转发",@"Method",@"Class",@"isKindOfClass",@"Block",@"线程管理",@"NSURLSession"];
    }
    
    return _titleArray;
}

- (NSArray<NSString *> *)controllerArray
{
    if (_controllerArray == nil)
    {
        _controllerArray = @[@"KeywordViewController",@"RunTimeViewController",
                             @"RunloopViewController",@"MessageViewController",
                             @"MethodViewController",@"ClassViewController",
                             @"IsKindClassViewController",@"BlockViewController",
                             @"ThreadManagerViewController",@"URLSessionTableViewController"];
    }
    
    return _controllerArray;
}

@end
